package com.xckj.cycling.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.xckj.cycling.framework.lazyGlobalViewModel
import com.junca.android.service.Request
import com.mapbox.navigation.utils.internal.toPoint
import com.xckj.cycling.framework.model.Response
import com.xckj.cycling.model.RouteLineListModel
import com.xckj.cycling.repository.MapRepository
import com.xckj.cycling.utils.addLocation
import kotlinx.coroutines.launch

class RouteLineViewModel : ViewModel() {

    private val mapViewModel: MapViewModel by lazyGlobalViewModel()
    private val repository = MapRepository()
    fun loadRoute(
        page: Int,
        size: Int = 20,
        type: String = "private",
        keyword: String = "",
        callback: suspend (Response<RouteLineListModel>) -> Unit
    ) {
        viewModelScope.launch {
            if (mapViewModel.lastRecordPoint == null) {
                val l = repository.location()
                if (l != null) {
                    mapViewModel.locationChanged(l.toPoint())
                    mapViewModel.locationChanged(l)
                }
            }
            val r = Request.postSuspend<RouteLineListModel>(
                "qixing/cycling/privateList",
                HashMap<String, Any>().apply {
                    put("page", page)
                    put("size", size)
                    put("type", type)
                    put("key", keyword)
                    addLocation()
                })
            callback.invoke(r)
        }
    }

    fun loadRouteDetail(id: Int, callback: (Response<RouteLineListModel>) -> Unit) {
        viewModelScope.launch {
            val r = Request.postSuspend<RouteLineListModel>(
                "qixing/cycling/privateDetail",
                HashMap<String, Any>().apply {
                    put("id", id)
                    mapViewModel.lastRecordLocation?.let {
                        put("lat", it.latitude)
                        put("lng", it.longitude)
                    }
                })
            callback.invoke(r)
        }
    }


    fun collect(id: Int, callback: (Response<Any>) -> Unit) {
        viewModelScope.launch {
            val r = Request.postSuspend<Any>(
                "qixing/cycling/collectionPrivateline",
                HashMap<String, Any>().apply {
                    put("id", id)
                })
            callback.invoke(r)
        }
    }

    fun praise(id: Int, callback: (Response<Any>) -> Unit) {
        viewModelScope.launch {
            val r = Request.postSuspend<Any>(
                "qixing/cycling/praisePrivateline",
                HashMap<String, Any>().apply {
                    put("id", id)
                })
            callback.invoke(r)
        }
    }

    fun cancelPraise(id: Int, callback: (Response<Any>) -> Unit) {
        viewModelScope.launch {
            val r = Request.postSuspend<Any>(
                "qixing/cycling/cancelPraiseline",
                HashMap<String, Any>().apply {
                    put("id", id)
                })
            callback.invoke(r)
        }
    }

    fun cancelCollect(id: Int, callback: (Response<Any>) -> Unit) {
        viewModelScope.launch {
            val r = Request.postSuspend<Any>(
                "qixing/cycling/cancelCollectionline",
                HashMap<String, Any>().apply {
                    put("id", id)
                })
            callback.invoke(r)
        }
    }

    fun addComment(
        id: Int,
        recid: String? = null,
        content: String = "",
        callback: (Response<Any>) -> Unit
    ) {
        viewModelScope.launch {
            val r = Request.postSuspend<Any>(
                "qixing/cycling/commentsPrivateline",
                HashMap<String, Any>().apply {
                    put("id", id)
                    if (recid != null) {
                        put("recid", recid)
                    }
                    put("text", content)
                })
            callback.invoke(r)
        }
    }
}